Functions.php
<?php
namespace Tlf\Scrawl;
class BashFunctionsExt extends \Tlf\Scrawl\ExtensionType\DefaultExt /*\Tlf\Scrawl\Extension*/ {
protected $regexes = [
'function' => '/\ *function\ *([a-zA-Z\_].+)\(\)/'
];
protected $funcList = [];
public function __construct($scrawl){
parent::__construct($scrawl);
}
public function onSourceFileFound($srcFile){
if ($srcFile->ext!='bash')return;
$dir = dirname($srcFile->relPath);
if ($dir=='code/core'){
$this->coreDirFile($srcFile);
}
}
public function coreDirFile($srcFile){
$content = $srcFile->content();
$lexer = new \Tlf\Lexer();
$lexer->addGrammar(new \Tlf\Lexer\BashGrammar());
$ast = $lexer->lex($srcFile->path);
unset($ast->source);
$tree = $ast->getTree();
$start = 'code/core/';
$len = strlen($start);
if (substr($srcFile->relPath, 0,$len)==$start){
if (!isset($tree['function'])){
$relPath = $srcFile->relPath;
echo "There was a parsing error & functions were not found for $relPath";
print_r($tree);
exit;
}
foreach ($tree['function'] as $func){
$this->scrawl->addOutput('bent_function', $func['name'], $func);
}
}
}
public function onSourceFilesDone(){
$funcs = $this->scrawl->getOutputs('bent_function');
$byGroup = [];
foreach ($funcs as $f){
$parts = explode('_', $f['name']);
$group = $parts[0];
if ($group==$f['name'])$byGroup[$group]['main']=$f;
else $byGroup[$group][] = $f;
}
$mains = [];
foreach ($byGroup as $group => $list){
$help = [];
if (!isset($list['main'])){
$list['main'] = [
'type'=>'function',
'name'=>$group,
'docblock'=>[
'type'=>'docblock',
'src'=>'--',
'description'=>'--',
],
];
}
$f = $list['main'];
unset($list['main']);
array_unshift($list,$f);
$mains[$f['name']] = $f;
$descript = $f['docblock']['tip'] ?? $f['docblock']['description'] ?? '';
$help[] = "# [bent $group] \${cOff}- $descript";
foreach ($list as $function){
$parts = explode('_', $function['name']);
$help[] = '${help_mode} '.implode(" ", $parts);
$help[] = implode(" ", count($parts)>1 ? array_slice($parts,1) : $parts);
$help[] = $function['docblock']['tip'] ?? $function['docblock']['description'] ?? '';
}
$help = array_map(function($v){return "\"$v\"";}, $help);
$helpStr ='prompt_choose_function '.implode(" ", $help);
$this->scrawl->addOutput('file', '../code/help/'.$group.'.bash', $helpStr);
}
uasort($mains,
function($a, $b){
$orderA = (int)($a['docblock']['order'] ?? 9999);
$orderB = (int)($b['docblock']['order'] ?? 9999);
if ($orderA > $orderB)return 1;
else if ($orderA == $orderB)return 0;
else return -1;
},
);
$help = [];
unset ($mains['']);
foreach ($mains as $name=>$function){
$parts = explode('_', $function['name']);
$help[] = '${help_mode} '.implode(" ", $parts);
$help[] = implode(" ", array_slice($parts, 0,1));
$help[] = $function['docblock']['tip'] ?? $function['docblock']['description'] ?? '';
}
$help = array_map(function($v){return "\"$v\"";}, $help);
$helpStr = 'prompt_choose_function '.implode(" ", $help);
$this->scrawl->addOutput('file', '../code/help/help_groups.bash', $helpStr);
}
}